﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.Product
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                string ID = Request.QueryString["ID"] ?? String.Empty;
                if (ID == String.Empty)
                {
                    Response.Redirect("~/Admin/Product/Index.aspx");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        LoadData();
                    }
                }
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }
            long ID = long.Parse(Request.QueryString["ID"]);
            SAN_PHAM sanPham = SanPhamModels.GetByID(ID);
            if (sanPham != null)
            {
                txtBrand.Value = sanPham.ThuongHieu;
                txtContent.Value = sanPham.MoTaChiTiet;
                txtInfoMore.Value = sanPham.ThongTinThem;
                txtMetaDescription.Value = sanPham.MetaDescription;
                txtMetaKeyword.Value = sanPham.MetaKeyword;
                txtMetaTitle.Value = sanPham.MetaTitle;
                txtPrice.Value = sanPham.DonGia.ToString("###,###");
                txtProductName.Value = sanPham.TenSanPham;
                txtQuantity.Value = sanPham.SoLuong.ToString();
                txtSummary.Value = sanPham.MoTaTomTat;
                lstProductType.InnerHtml = LoaiSanPhamModels.ShowSelectProductTypeCheck(sanPham.IDLoaiSanPham);
            }
            ShowImagesForDelete(ID);
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {

            //HinhSanPhamModels.Delete(2, ResolveClientUrl("~/images/products/"));

            if (txtProductName.Value != "")
            {
                long ID = long.Parse(Request.QueryString["ID"]);
                SAN_PHAM sanPham = SanPhamModels.GetByID(ID);
                sanPham.DonGia = long.Parse(txtPrice.Value.Replace(",", ""));
                sanPham.IDLoaiSanPham = long.Parse(Request.Form["productType"]);
                sanPham.MoTaChiTiet = txtContent.Value;
                sanPham.MoTaTomTat = txtSummary.Value;
                sanPham.MetaDescription = txtMetaDescription.Value;
                sanPham.MetaKeyword = txtMetaKeyword.Value;
                sanPham.MetaTitle = txtMetaTitle.Value;
                sanPham.NgaySua = DateTime.Now;
                sanPham.SoLuong = int.Parse(txtQuantity.Value);
                sanPham.TenSanPham = txtProductName.Value;
                sanPham.ThuongHieu = txtBrand.Value;
                sanPham.ThongTinThem = txtInfoMore.Value;
                if (fUpload.HasFiles)
                {
                    List<HINH_SAN_PHAM> lstImages = new List<HINH_SAN_PHAM>();
                    foreach (HttpPostedFile uploadedFile in fUpload.PostedFiles)
                    {
                        string fileNew = Guid.NewGuid().ToString() + Path.GetExtension(uploadedFile.FileName);
                        uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/images/products/"), fileNew));
                        lstImages.Add(new HINH_SAN_PHAM() { Hinh = fileNew, IDSanPham = sanPham.ID });
                    }
                    HinhSanPhamModels.Add(lstImages);
                }

                bool status = SanPhamModels.Update(sanPham);
                if (status)
                {
                    Session["Info"] = "<div class='alert alert-success'>Cập nhật sản phẩm thành công !!!</div>";
                    Response.Redirect("~/Admin/Product/Index.aspx");
                }
                else
                {
                    info.InnerHtml = "<div class='alert alert-danger'>Đã xảy ra lỗi, vui lòng thử lại !!!</div>";
                }
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Vui lòng nhập đủ thông tin !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Product/Index.aspx");
        }

        private void ShowImagesForDelete(long IDSanPham)
        {
            StringBuilder str = new StringBuilder();
            List<HINH_SAN_PHAM> data = HinhSanPhamModels.GetByIDProduct(IDSanPham);


            str.Append("<button data-toggle=\"modal\" data-target=\"#modalShowImageProduct\" class=\"btn btn-danger\" type=\"button\">Xóa Hình Sản Phẩm</button>");
            str.Append("<div aria-hidden=\"true\" aria-labelledby=\"myModalLabel\" role=\"dialog\" tabindex=\"-1\" id=\"modalShowImageProduct\" class=\"modal fade\" style=\"display: none;\">");
            str.Append("<div class=\"modal-dialog\">");
            str.Append("<div class=\"modal-content\">");
            str.Append("<div class=\"modal-header\">");
            str.Append("<button aria-hidden=\"true\" data-dismiss=\"modal\" class=\"close\" type=\"button\">×</button>");
            str.Append("<h4 id=\"myModalLabel\" class=\"modal-title\">Xóa Hình Sản Phẩm</h4>");
            str.Append("</div>");
            str.Append("<div class=\"modal-body\">");

            if (data != null && data.Count > 0)
            {
                foreach (var item in data)
                {
                    string urlImage = ResolveClientUrl("~/images/products/" + item.Hinh);
                    str.Append("<div class=\"col-xs-6\" id=\"deleteImgID"+item.ID+"\">");
                    str.Append("<div class=\"panel panel-red\">");
                    str.Append("<div class=\"panel-heading\">");
                    str.Append("<div class=\"row\">");
                    str.Append("<img width=\"250\" height=\"250\" src=\"" + urlImage + "\" />");
                    str.Append("</div>");
                    str.Append("</div>");
                    str.Append("<a href=\"#\" onclick=\"DeleteImageProduct("+item.ID+","+item.IDSanPham+")\">");
                    str.Append("<div class=\"panel-footer\">");
                    str.Append("<span class=\"pull-left\">Xóa Hình Sản Phẩm</span>");
                    str.Append("<span class=\"pull-right\"><i class=\"fa fa-arrow-circle-right\"></i></span>");
                    str.Append("<div class=\"clearfix\"></div>");
                    str.Append("</div>");
                    str.Append("</a>");
                    str.Append("</div>");
                    str.Append("</div>");
                }
            }
            str.Append("</div>");
            str.Append("<div class=\"modal-footer\" style=\"clear:both\">");
            str.Append("<button data-dismiss=\"modal\" class=\"btn btn-danger\" type=\"button\">Close</button>");
            str.Append("</div>");
            str.Append("</div>");
            str.Append("</div>");
            str.Append("</div>");
            lstImages.InnerHtml = str.ToString();
        }
    }
}